package cn.guoxuepeng.hrm.controller;

import cn.guoxuepeng.hrm.domain.CourseByKeyVo;
import cn.guoxuepeng.hrm.domain.CourseAddES;
import cn.guoxuepeng.hrm.repository.CourseAddESRepository;
import cn.guoxuepeng.hrm.util.AjaxResult;
import cn.guoxuepeng.hrm.util.PageList;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ElasticsearchController {

    @Autowired
    private CourseAddESRepository courseAddESRepository;

    /**
     * 添加课程到es中
     * @param list
     * @return
     */
    @PostMapping("/addBatch")
    public AjaxResult add(@RequestBody List<CourseAddES> list){
        try {
            courseAddESRepository.saveAll(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败!"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public AjaxResult delete(@RequestBody List<Long> ids){
        try {
            for (Long id : ids) {
                courseAddESRepository.deleteById(id);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败!"+e.getMessage());
        }
    }

    /**
     * 根据关键字查询课程并返回
     * @param courseByKeyVo
     * @return
     */
    @PostMapping("/findCourseByKey")
    public AjaxResult findCourseByKey(@RequestBody CourseByKeyVo courseByKeyVo){
        try {
            NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            //匹配all
            if(courseByKeyVo.getKeyword()!=null){
                boolQueryBuilder.must(new MatchQueryBuilder("all",courseByKeyVo.getKeyword()));
            }
            if (courseByKeyVo.getCourseType()!=null){
                boolQueryBuilder.must(new MatchQueryBuilder("courseTypeId",courseByKeyVo.getCourseType()));
            }
            if(courseByKeyVo.getTenantId()!=null){
                boolQueryBuilder.must(new MatchQueryBuilder("tenantId",courseByKeyVo.getTenantId()));
            }
            //价格的区间
            List<QueryBuilder> filter = boolQueryBuilder.filter();
            if(courseByKeyVo.getMaxPrice()==0&&courseByKeyVo.getMaxPrice()==null) {
                filter.add(new RangeQueryBuilder("price").gte(courseByKeyVo.getMinPrice()));
                builder.withQuery(boolQueryBuilder);
            }else {
                filter.add(new RangeQueryBuilder("price").lte(courseByKeyVo.getMaxPrice()).gte(courseByKeyVo.getMinPrice()));
                builder.withQuery(boolQueryBuilder);
            }
            //当前页数和每页条数
            builder.withPageable(PageRequest.of(courseByKeyVo.getPageNum()-1,courseByKeyVo.getPageSize()));
            Page<CourseAddES> courseAddES = courseAddESRepository.search(builder.build());
            PageList<CourseAddES> pageList = new PageList<>();
            pageList.setTotal(courseAddES.getTotalElements());
            List<CourseAddES> content = courseAddES.getContent();
            pageList.setRows(content);
            return AjaxResult.me().setSuccess(true).setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败!"+e.getMessage());
        }
    }
}

